<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;

class MyLogicClassController extends BaseController {

	public function index() {
		$studentId = I('session.uid');
		$logicClasseIds = M('LogicClassStudent')
			->where(array('student_id' => $studentId))
			->getField('logic_class_id', true);
		$logicClasses = D('LogicClass')
			->where(array('id' => array('in', $logicClasseIds)))
			->relation(array('teacher', 'course'))
			->select();
		$this->assign('logic_classes', $logicClasses);
		$this->display();
	}

	public function join() {
		$studentId = I('session.uid');
		$myLogicClasseIds = M('LogicClassStudent')
			->where(array('student_id' => $studentId))
			->getField('logic_class_id', true);
		$logicClasses = M('LogicClass')
			->where(array('id' => array('not in', $myLogicClasseIds)))
			->select();
		$this->assign('logic_classes', $logicClasses);
		$this->display();
	}

	public function joinPost() {
		if(!IS_POST) {
			$this->redirect('MyLogicClass/index');
		}
		$logicClassId = I('post.logic_class_id');
		if(!M('LogicClass')->where(array('id' => $logicClassId))->find()) {
			$this->error('逻辑班级值有误', U('MyLogicClass/join'));
		}
		$data['logic_class_id'] = $logicClassId;
		$data['student_id'] = I('session.uid');
		if(false === M('LogicClassStudent')->data($data)->add()) {
			$this->error('数据写入失败！', U('MyLogicClass/join'));
		}
		$this->success('加入成功！', U('MyLogicClass/index'), 0);
	}

}
